package com.gzxx.controller;

import com.gzxx.domain.base.BaseDto;
import com.gzxx.domain.base.BaseVo;
import com.gzxx.domain.converter.SysDeptConverter;
import com.gzxx.domain.dto.SysDeptCreateDto;
import com.gzxx.domain.dto.SysDeptQueryDto;
import com.gzxx.domain.dto.SysDeptUpdateDto;
import com.gzxx.domain.vo.SysDeptVo;
import com.gzxx.service.SysDeptService;
import com.gzxx.util.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * SysDeptController
 *
 * @author chenjing
 * @date 2020-09-22 17:43:19
 */
@Api(tags = "部门管理相关接口")
@RestController
@RequestMapping("depts")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @ApiOperation("部门列表")
    @PostMapping("/list")
    @PreAuthorize("@rbac.hasPermission('system:sysdepts:list')")
    public ServerResponse<SysDeptVo> list(@RequestBody SysDeptQueryDto queryDto) {
        List<SysDeptVo> list = sysDeptService.list(SysDeptConverter.MAPPER.convertQueryDto(queryDto));
        return ServerResponse.success(list);
    }

    /**
     * 详情
     */
    @ApiOperation("部门详情")
    @GetMapping("/detail")
    @PreAuthorize("@rbac.hasPermission('system:sysdepts:select')")
    public ServerResponse<SysDeptVo> detail(@RequestParam("id") Long id) {
        SysDeptVo sysDept = sysDeptService.getDetail(id);
        return ServerResponse.success(sysDept);
    }

    /**
     * 新增
     */
    @ApiOperation("新增部门")
    @PostMapping("/add")
    @PreAuthorize("@rbac.hasPermission('system:sysdepts:add')")
    public ServerResponse save(@Valid @RequestBody SysDeptCreateDto createDto) {
        return sysDeptService.add(SysDeptConverter.MAPPER.convertCreateDto(createDto));
    }

    /**
     * 更新
     */
    @ApiOperation("编辑部门")
    @PostMapping("/update")
    @PreAuthorize("@rbac.hasPermission('system:sysdepts:update')")
    public ServerResponse update(@Valid @RequestBody SysDeptUpdateDto updateDto) {
        return sysDeptService.update(SysDeptConverter.MAPPER.convertUpdateDto(updateDto));
    }

    /**
     * 变更状态
     */
    @ApiOperation("变更部门状态")
    @PostMapping("/status")
    @PreAuthorize("@rbac.hasPermission('system:sysdepts:update:status')")
    public ServerResponse status(@Valid @RequestBody BaseDto baseDto) {
        return sysDeptService.changeStatus(SysDeptConverter.MAPPER.convertBaseDto(baseDto));
    }

    /**
     * 查询部门
     */
    @ApiOperation("查询部门 树形")
    @GetMapping("/types")
    public ServerResponse<BaseVo> types() {
        List<BaseVo> list = sysDeptService.treeList();
        return ServerResponse.success(list);
    }
}
